<template>
  <view>
    <view class="search-box">
      <uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
    </view>
    <!-- 搜索建议 -->
    <view class="sugg-list" v-if="searchResults.length !== 0">
     <view class="sugg-item"  v-for="(item,i) in searchResults" :key="i" @click="gotoDetail(item)">
       <view class="goods_name">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    
    <!--  搜索历史  -->
    <view class="history-box" v-else>
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clean"></uni-icons>
      </view>
      <view class="history-list">
        <uni-tag :text="item" v-for="(item, i) in histories" :key="i" @click="gotoGoodsList(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer: null,
        kw :'' ,
        // 搜索结果列表
        searchResults: [
            {
              "goods_id": 57332,
              "goods_name": "400毫升，海鲜食品冷藏冰柜 注入冰敷医用冰反复使用400毫升400毫升400毫升400毫升400毫升400毫升400毫升400毫升"
            },
            {
              "goods_id": 57194,
              "goods_name": "400毫升，海鲜食品冷藏冰柜 注入冰敷医用冰反复使用400毫升400毫升400400毫升400毫升毫升400毫升400毫升400毫升"
            }
        ],
        historyList:['a', 'app', 'Joyson','s']
      };
    },
    methods:{
      input(e){
        //  清除对应的延时器
        clearTimeout(this.timer)
         // 重新启动一个延时器，并把 timerId 赋值给 this.timer
        this.timer = setTimeout(() => {
           // 如果 500 毫秒内，没有触发新的输入事件，则为搜索关键词赋值
          this.kw = e.value
          // 根据关键词，查询搜索建议列表
          this.getSearchResults()
        },500)
      },
      async getSearchResults(){
        if(this.kw === ''){
          this.searchResults = []
          return
        }
        this.saveSearchHistory()
        // const { data:res } = await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.kw})
        // console.log(res);
        // if(res.meta.status  !== 200) return uni.$showMsg()
        // this.searchResults = res.message
      },
      gotoDetail(item){
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?goods_id='+ item.goods_id
        })
      },
      // 保存搜索关键词的方法
      saveSearchHistory(){
        // this.historyList.push(this.kw)
        // 1.将数组转化为set对象
        const set =new Set(this.historyList)
        // 2. 删除后添加 搜索相同的关键词
        set.delete(this.kw)
        set.add(this.kw)
        // 3. 将set对象转化为数组
        this.historyList = Array.from(set)
        // 将搜索历史持久化存储到本地
        uni.setStorageSync('kw',JSON.stringify(this.historyList))
      },
      clean(){
        this.historyList = []
        uni.setStorageSync('kw','[]')
      },
      gotoGoodsList(){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query='+ kw
        })
      }
    },
    computed:{
      //  计算属性 拼接历史关键词后 进行反转
      histories(){
        return [...this.historyList].reverse()
      }
    },
    onLoad(){
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
    }
  }
</script>

<style lang="scss">
  .search-box{
    position: sticky;
    top: 0;
    z-index: 999;
  }
  .sugg-list{
    padding: 0 5px;
  }
  .sugg-item{
    font-size: 14px;
    padding: 13px 0;
    border-bottom: 1px solid #efefef;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .goods_name{
    // 文本不换行
    white-space: nowrap;
    // 隐藏超出文本
    overflow: hidden;
    // 超出文本用...表示
    text-overflow: ellipsis;
    margin-right: 3px;
  }
  .history-box{
    padding: 0 5px;
  }
  .history-title{
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    font-size: 13px;
    border-bottom: 1px solid #efefef;
  }
  .history-list{
    display: flex;
    flex-wrap: wrap;
  }
  .uni-tag{
    margin-top: 5px;
    margin-left: 5px;
  }
</style>
